﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoreVnCore.DataAccessObject;
using StoreVnCore.Entity;
using StoreVnCore.ExceptionManager;
using System.Data;

namespace StoreVnCore.Business
{
    /// <summary>
    /// Business Layer of ContentCategoryBO.
    /// </summary>
    public class ContentCategoryBO
    {
        #region Basic Functions
        public ContentCategoryEntity AddNew(ContentCategoryEntity entity)
        {
            try
            {
                ContentCategoryDAO objDAO = new ContentCategoryDAO();
                return (ContentCategoryEntity)objDAO.Insert(entity);
            }
            catch (DataAccessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new BusinessException("ContentCategoryBO::AddNew::Error", ex);
            }
        }

        public ContentCategoryEntity Save(ContentCategoryEntity entity)
        {
            try
            {
                ContentCategoryDAO objDAO = new ContentCategoryDAO();
                return (ContentCategoryEntity)objDAO.Update(entity);
            }
            catch (DataAccessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new BusinessException("ContentCategoryBO::Save::Error", ex);
            }
        }

        public void Delete(int id)
        {
            try
            {
                ContentCategoryDAO objDAO = new ContentCategoryDAO();
                objDAO.Delete(id);
            }
            catch (DataAccessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new BusinessException("ContentCategoryBO::Delete::Error", ex);
            }
        }

        public ContentCategoryEntity SelectOne(int id)
        {
            try
            {
                ContentCategoryDAO objDAO = new ContentCategoryDAO();
                return (ContentCategoryEntity)objDAO.SelectOne(id);
            }
            catch (DataAccessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new BusinessException("ContentCategoryBO::SelectOne::Error", ex);
            }
        }

        public List<ContentCategoryEntity> SelectList()
        {
            try
            {
                ContentCategoryDAO objDAO = new ContentCategoryDAO();
                DataTable dt = objDAO.SelectAll();
                if (dt != null && dt.Rows.Count > 0)
                {
                    List<ContentCategoryEntity> list = new List<ContentCategoryEntity>();
                    foreach (DataRow dr in dt.Rows)
                    {
                        ContentCategoryEntity entity = new ContentCategoryEntity();
                        //Id
                        if (dr[FieldNames.ContentCategory.Id] != null && dr[FieldNames.ContentCategory.Id].ToString() != "")
                            entity.Id = (int)dr[FieldNames.ContentCategory.Id];

                        //TypeOfContent
                        if (dr[FieldNames.ContentCategory.TypeOfContent] != null && dr[FieldNames.ContentCategory.TypeOfContent].ToString() != "")
                            entity.TypeOfContent = (string)dr[FieldNames.ContentCategory.TypeOfContent];

                        //Name
                        if (dr[FieldNames.ContentCategory.Name] != null && dr[FieldNames.ContentCategory.Name].ToString() != "")
                            entity.Name = (string)dr[FieldNames.ContentCategory.Name];

                        //Description
                        if (dr[FieldNames.ContentCategory.Description] != null && dr[FieldNames.ContentCategory.Description].ToString() != "")
                            entity.Description = (string)dr[FieldNames.ContentCategory.Description];

                        list.Add(entity);
                    }
                    return list;
                }
                return null;
            }
            catch (DataAccessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new BusinessException("ContentCategoryBO::SelectList::Error", ex);
            }
        }

        public DataTable SelectAll()
        {
            try
            {
                ContentCategoryDAO objDAO = new ContentCategoryDAO();
                return objDAO.SelectAll();
            }
            catch (DataAccessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new BusinessException("ContentCategoryBO::SelectAll::Error", ex);
            }
        }

        public DataTable SelectByTypeOfContent(string TypeOfContent)
        {
            try
            {
                ContentCategoryDAO objDAO = new ContentCategoryDAO();
                return objDAO.SelectByTypeOfContent(TypeOfContent);
            }
            catch (DataAccessException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new BusinessException("ContentCategoryBO::SelectByTypeOfContent::Error", ex);
            }
        }

        #endregion
    }
}
